<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>value_count 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-valuecount-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-valuecount-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-valuecount-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-valuecount-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-valuecount-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">value_count 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics-top-metrics.html">« top_metrics 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket.html">桶(bucket)聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-valuecount-aggregation"></a>值数量(value_count)聚合
</h2>
</div></div></div>
<p>
一个 <code class="literal">single-value</code>(单值) 度量聚合，计算从聚合文档中提取的值的数量。

这些值可以从文档的指定字段中提取，也可以由提供的脚本生成。

通常，此聚合器将与其他单值聚合一起使用。

例如，在计算 <code class="literal">avg</code>(平均值) 时，人们可能会对计算平均值的值的数量感兴趣。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs" : {
        "types_count" : { "value_count" : { "field" : "type" } }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/409.console"></div>
<p>响应为：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "types_count": {
            "value": 7
        }
    }
}</pre>
</div>
<p>
聚合的名称(上面的<code class="literal">types_count</code>)也用作 key，通过它可以从返回的响应中检索聚合结果。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_script_14"></a>script (脚本)
</h3>
</div></div></div>
<p>计算脚本生成的值：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs" : {
        "type_count" : {
            "value_count" : {
                "script" : {
                    "source" : "doc['type'].value"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/410.console"></div>
<p>
这将把 <code class="literal">script</code> 参数解释为一个<code class="literal">inline</code>(内联) 脚本，使用 <code class="literal">painless</code>(无痛) 脚本语言，没有脚本参数。

要使用存储的脚本，请使用以下语法:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs" : {
        "types_count" : {
            "value_count" : {
                "script" : {
                    "id": "my_script",
                    "params" : {
                        "field" : "type"
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/411.console"></div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics-top-metrics.html">« top_metrics 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket.html">桶(bucket)聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>